<template>
  <div class="dialog">
    <el-dialog
      :title="dialog.title"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :modal-append-to-body="false"
      :visible.sync="dialog.show"
    >
      <el-form ref="ServerForms" :model="ServerForms" label-width="150px">
        <el-form-item v-if="false" label="id">
          <el-input
            v-model.number="ServerForms.id"
            placeholder="请输入服务器ID"
          ></el-input>
        </el-form-item>
        <el-form-item label="服务器Id">
          <el-input
            v-model.number="ServerForms.serverId"
            placeholder="请输入服务器ID"
          ></el-input>
        </el-form-item>

        <el-form-item label="服务器名称:">
          <el-input
            v-model="ServerForms.serverName"
            placeholder="请输入服务器名称"
          ></el-input>
        </el-form-item>

        <el-form-item label="登录IP：">
          <el-input
            v-model="ServerForms.serverIp"
            placeholder="请输入登录IP地址"
          ></el-input>
        </el-form-item>
        <el-form-item label="登录端口号:">
          <el-input
            v-model.number="ServerForms.loginPort"
            placeholder="请输入登录端口号"
          ></el-input>
        </el-form-item>
        <el-form-item label="网关IP：">
          <el-input
            v-model="ServerForms.gateIp"
            placeholder="请输入网关IP地址"
          ></el-input>
        </el-form-item>
        <el-form-item label="网关端口号:">
          <el-input
            v-model.number="ServerForms.gatePort"
            placeholder="请输入网关端口号"
          ></el-input>
        </el-form-item>
        <el-form-item label="gmUrl:">
          <el-input
            v-model="ServerForms.gmUrl"
            placeholder="请输入gmUrl"
          ></el-input>
        </el-form-item>
        <el-form-item label="新服图标：">
          <el-select
            v-model="ServerForms.newIcon"
            placeholder="请选择是否存在新服图标"
          >
            <el-option label="有图标" value="1"></el-option>
            <el-option label="没有图标" value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="开服时间:">
          <el-date-picker
            :disabled="dialog.mailSearch"
            v-model="ServerForms.openTime"
            type="datetime"
            value-format="timestamp"
            placeholder="选择玩家注册起始时间"
          ></el-date-picker>
          <br/>
          <span style="color:red;font-size:12px;">注意：该时间是您开服的时间，请慎重填写,该时间如果不填，服务器无法启用。</span>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleclose()">取 消</el-button>
        <el-button type="primary" @click="handleAddEdit()">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  name: "ServerDialog",
  // 获取父亲传过来的值
  props: {
    dialog: {
      type: Object,
      default: () => {},
    },
    ServerForm: {
      type: Object,
      default: () => {},
    },
  },
  data() {
    return {
      ServerForms: this.ServerForm,
      newIcons: [
        { title: "有图标", value: 1 },
        { title: "没有图标", value: 0 },
      ],
    };
  },
  methods: {
    handleclose() {
      this.dialog.show = false;
    },
    handleAddEdit() {
      if(this.dialog.title=="新建服务器"){
         if (this.ServerForms.newIcon == "") {
            this.$message({type:"warning",message:"请选择新服图标"});
            return;
          } else if (this.ServerForms.serverId == "") {
            this.$message({type:"warning",message:"请输入服务器id"});
            return;
          } else if (this.ServerForms.serverName == "") {
            this.$message({type:"warning",message:"请输入服务器名称"});
            return;
          } else if (this.ServerForms.serverIp == "") {
            this.$message({type:"warning",message:"请输入登录Ip"});
            return;
          } else if (this.ServerForms.loginPort == "") {
            this.$message({type:"warning",message:"请输入登录的端口号"});
            return;
          }else if (this.ServerForms.gmUrl == "") {
            this.$message({type:"warning",message:"请输入gmUrl"});
            return;
          }else if (this.ServerForms.gateIp == "") {
            this.$message({type:"warning",message:"请输入网关ip"});
            return;
          } else if (this.ServerForms.gatePort == "") {
            this.$message({type:"warning",message:"请输入网关端口号"});
            return;
          }else {
            let obj = {
              id: this.ServerForms.id,
              serverId: this.ServerForms.serverId,
              serverName: this.ServerForms.serverName,
              serverIp: this.ServerForms.serverIp,
              loginPort: this.ServerForms.loginPort,
              serverIp: this.ServerForms.serverIp,
              loginPort: this.ServerForms.loginPort,
              newIcon: this.ServerForms.newIcon,
              gmUrl: this.ServerForms.gmUrl,
              gateIp:this.ServerForms.gateIp,
              gatePort:this.ServerForms.gatePort,
              openTime:this.ServerForms.openTime/1000
            };
            this.dialog.show = false;
            this.$emit("saveServer", obj);
          }
      }else{
         if (this.ServerForms.newIcon == ""||this.ServerForms.newIcon==undefined) {
            this.$message({type:"warning",message:"请选择新服图标"});                     
            return;
          } else if (this.ServerForms.serverId == ""||this.ServerForms.serverId==undefined) {
            this.$message({type:"warning",message:"请输入服务器id"});
            return;
          } else if (this.ServerForms.serverName == ""||this.ServerForms.serverName==undefined) {
            this.$message({type:"warning",message:"请输入服务器名称"});
            return;
          } else if (this.ServerForms.serverIp == ""||this.ServerForms.serverIp==undefined) {
            this.$message({type:"warning",message:"请输入服务器Ip"});
            return;
          } else if (this.ServerForms.loginPort == ""||this.ServerForms.loginPort==undefined) {
            this.$message({type:"warning",message:"请输入登录的端口号"});
            return;
          } else if (this.ServerForms.gmUrl == ""||this.ServerForms.gmUrl==undefined) {
            this.$message({type:"warning",message:"请输入gmUrl"});
            return;
          }else if (this.ServerForms.gateIp == ""||this.ServerForms.gateIp==undefined) {
            this.$message({type:"warning",message:"请输入网关ip"});
            return;
          } else if (this.ServerForms.gatePort == ""||this.ServerForms.gatePort==undefined) {
            this.$message({type:"warning",message:"请输入网关端口号"});
            return;
          }else{
            let obj = {
              id: this.ServerForms.id,
              serverId: this.ServerForms.serverId,
              serverName: this.ServerForms.serverName,
              serverIp: this.ServerForms.serverIp,
              loginPort: this.ServerForms.loginPort,
              serverIp: this.ServerForms.serverIp,
              loginPort: this.ServerForms.loginPort,
              newIcon: this.ServerForms.newIcon,
              gmUrl: this.ServerForms.gmUrl,
              gateIp:this.ServerForms.gateIp,
              gatePort:this.ServerForms.gatePort,
              openTime:this.ServerForms.openTime/1000,
              enabled:this.ServerForms.enabled
            };
            this.dialog.show = false;
            this.$emit("saveServer", obj);
          }
      }
    },
  },
};
</script>
<style scoped>
.upload {
  width: 20px;
  height: 20px;
  margin-top: 10px;
}
.btn {
  padding: 10px;
  margin-left: 50%;
}
</style>